package be.wiim.rpi;

 import com.pi4j.io.gpio.GpioController;  
 import com.pi4j.io.gpio.GpioFactory;  
 import com.pi4j.io.gpio.GpioPinDigitalOutput;  
 import com.pi4j.io.gpio.PinState;  
 import com.pi4j.io.gpio.RaspiPin;  
 /**  
  *  
  * @author Wim   
  */  
 public class StepperMotor {  
     
   private final GpioController fGpioController = GpioFactory.getInstance();  
   private GpioPinDigitalOutput fPin0,fPin1,fPin2,fPin3;  
   public static StepperMotor fStepperMotor; 
   
   public StepperMotor() 
   {  
    fPin0 = fGpioController.provisionDigitalOutputPin(RaspiPin.GPIO_00, "pin00", PinState.LOW);  
    fPin1 = fGpioController.provisionDigitalOutputPin(RaspiPin.GPIO_01, "pin01", PinState.LOW);  
    fPin2 = fGpioController.provisionDigitalOutputPin(RaspiPin.GPIO_02, "pin02", PinState.LOW);  
    fPin3 = fGpioController.provisionDigitalOutputPin(RaspiPin.GPIO_03, "pin03", PinState.LOW);  
    fStepperMotor = this;  
   }  
   
   public static StepperMotor getInstance()  
   {  
     if(fStepperMotor == null)  
       fStepperMotor = new StepperMotor();  
     return fStepperMotor;  
   }  
   
   public void spinLeft() throws InterruptedException  
   {  
     GpioPinDigitalOutput[] vGpioPins = { fPin0,fPin1,fPin2,fPin3 };  
     GpioPinDigitalOutput vOldPin = null, vNewPin = null;  
     int counter1 = 0;  
     while(counter1 < 15)  
     {  
       int counter = 0;  
       counter1++;  
       while(counter < vGpioPins.length)  
       {  
         switch(counter)  
         {  
           case 0:  
             vOldPin = vGpioPins[vGpioPins.length-1];  
             vNewPin = vGpioPins[counter];  
             break;  
           case 1:  
             vOldPin = vGpioPins[counter-1];  
             vNewPin = vGpioPins[counter];  
             break;  
           case 2:  
             vOldPin = vGpioPins[counter-1];  
             vNewPin = vGpioPins[counter];  
             break;  
           case 3:  
             vOldPin = vGpioPins[counter-1];  
             vNewPin = vGpioPins[counter];  
          break;  
         }  
         vOldPin.setState(PinState.LOW);  
         vNewPin.setState(PinState.HIGH);  
         Thread.sleep(7);  
         counter++;  
       }  
     }  
   }  
     public void spinRight() throws InterruptedException  
   {  
     GpioPinDigitalOutput[] vGpioPins = { fPin3,fPin2,fPin1,fPin0 };  
     GpioPinDigitalOutput vOldPin = null, vNewPin = null;  
     while(true)  
     {  
       int counter = 0;  
       while(counter < vGpioPins.length)  
       {  
         switch(counter)  
         {  
           case 0:  
             vOldPin = vGpioPins[vGpioPins.length-1];  
             vNewPin = vGpioPins[counter];  
             break;  
           case 1:  
             vOldPin = vGpioPins[counter-1];  
             vNewPin = vGpioPins[counter];  
             break;  
           case 2:  
             vOldPin = vGpioPins[counter-1];  
             vNewPin = vGpioPins[counter];  
             break;  
           case 3:  
             vOldPin = vGpioPins[counter-1];  
             vNewPin = vGpioPins[counter];  
          break;  
         }  
         vOldPin.setState(PinState.LOW);  
         vNewPin.setState(PinState.HIGH);  
         Thread.sleep(7);  
         counter++;  
       }  
     }  
   }  
   public void stop()  
   {  
     fPin0.setState(PinState.LOW);  
     fPin1.setState(PinState.LOW);  
     fPin2.setState(PinState.LOW);  
     fPin3.setState(PinState.LOW);  
   }  
 }  